﻿public class Solution {
    public int[] DailyTemperatures(int[] temperatures) 
    {
        // 单调栈
        Stack<int> stack = new Stack<int>();
        
        // 结果数组
        int[] result = new int[temperatures.Length];
        if (temperatures is null || temperatures.Length == 0) return result;
        
        // 填充数组
        for (int i = 0; i < temperatures.Length; i++)
        {
            while (stack.Count > 0)
            {
                int top = stack.Peek();
                if (temperatures[top] < temperatures[i])
                {
                    stack.Pop();
                    result[top] = i - top;
                }
                else
                {
                    break;
                }
            }
            stack.Push(i);
        }

        return result;
    }

    public static void Main(string[] args)
    {
        int[] temperatures = [73, 74, 75, 71, 69, 72, 76, 73];
        Solution solution = new Solution();
        int[] result = solution.DailyTemperatures(temperatures);

        foreach (int res in result)
        {
            Console.Write($"{res} ");
        }
        Console.WriteLine();
    }
}